package com.hechi.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hechi.demo.dto.ResultResponse;
import com.hechi.demo.entity.Doctor;
import com.hechi.demo.entity.Users;
import com.hechi.demo.exception.TijianExceptionEnum;
import com.hechi.demo.service.DoctorServiceImpl;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class DoctorController {
    @Autowired
    DoctorServiceImpl doctorService;

    /*
    接口2
    接口3
    接口4
    .。。。。。。
     */

    //只是测试前端访问后端接口时确实有传token过来
    @RequestMapping("/test")  //假设这个接口的功能： 查询所有未归档订单
    public ResultResponse test(HttpServletRequest request,HttpServletResponse response){

        Doctor doctor=(Doctor)request.getAttribute("doctor");

        if(doctor!=null){
            //doctorService.....
            System.out.println("校验通过，调用service方法处理业务问题。。。。。。。。。。。。。。。。");
        }else{
            // throw new TijianException()
        }


        return new ResultResponse(ResultResponse.SUCCESS_CODE,ResultResponse.SUCCESS_DESC,null);
    }

    @RequestMapping("/login")
    public ResultResponse login(@RequestBody Doctor doctor, HttpServletResponse response){


        System.out.println("doctor message:"+doctor.getDocCode()+","+doctor.getPassword());

        ResultResponse resultResponse=doctorService.login(doctor);

        //我们在此处做个判断：如果登录成功，把医生信息打包加密给到前端
        //JWT；json格式 解决web应用的令牌问题  登录校验  用户身份校验 第一有没有登录  第二你的用户类型的问题
        Doctor doctor_result=(Doctor) resultResponse.getData();
        if (resultResponse.getCode() == 200) {
            Map<String, Object> map = new HashMap<>();

            try {
                map.put("currentdoctor", new ObjectMapper().writeValueAsString(doctor_result));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

            JwtBuilder builder = Jwts.builder()
                    .setId(UUID.randomUUID().toString())
                    .setIssuedAt(new Date())
                    .signWith(SignatureAlgorithm.HS256, "123456----")    //随便写,字符越奇葩越好
                    .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 20))     //改成3，方便测试
                    .setClaims(map).setSubject(doctor_result.getDocId()+"");
            String key = builder.compact();
            response.setHeader("token", key);
            System.out.println("返回数据给前端。。。。。。。。。。。。。。");
        }


        return resultResponse;

    }

}
